import { request } from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime'
// index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // getAllArtical API data
    Cates: [],
    Quotes: "xxx",
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {},
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/1dY8eIPSZsXbViH.jpg'
    }, {
      id: 1,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/LbHNREVwl1WKrqd.jpg',
    }, {
      id: 2,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/FvTj7YPfum9C35Z.jpg'
    }, {
      id: 3,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/pcOJweh986sKrjY.jpg'
    }, {
      id: 4,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/eRcIT7jJPSZrgU3.jpg'
    }, {
      id: 5,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/6P3upJCn4gocrWD.jpg'
    }, {
      id: 6,
      type: 'image',
      url: 'https://s2.loli.net/2022/01/30/E12ODwGdYC8XxoJ.jpg'
    }],
  },
  /**
   * Login Method
   */
  getUserProfile: function () {
    var that = this;
    wx.getUserProfile({
        desc: '用于完善用户资料',
        success: (res) => {
          if (res.errMsg == "getUserProfile:ok") {
            wx.setStorageSync('userInfo',res.userInfo)
            app.globalData.userInfo = res.userInfo
            that.setData({
              userInfo: res.userInfo,
              hasUserInfo: true,
            })
          }
        } ,fail: err => {
          wx.showToast({
            title: '授权后才能体验更多功能哦',
            icon: 'none',
            duration: 3000
          })
        },
    })
  },
  /**
   * Github 跳转
   */
  onTapGithub() {
    wx.navigateTo({
      url: '/pages/github/github',
    })
  },
  /**
   * Moment 跳转
   */
  onTapMoment() {
    wx.navigateTo({
      url: '/pages/moment/moment',
    })
  },

  handleGetUserInfo(e) {
    console.log(e)
  },
  showModal(e) {
    console.log(e)
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  tabSelect(e) {
    console.log(e);
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可
    // get allArtical data
    this.getAllArticalData();
  },
  /**
   * 获取 Artical API 数据
   */
  async getAllArticalData() {
    // 1. 使用 es7 async await 发送请求
    const res = await request({url: "/allArtical"});
    console.log(res.data.data)

    // 2. 赋值给 AppData
    let Cates = res.data.data
    this.setData({
        Cates
    })
  },

  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})